< previous page page_183 next page >

Page 183
Visual Basic supports only one string operator, the concatenation operator. However, several string functions are built in to the Visual Basic language, and you can use them to manipulate strings. Some of these functions are as follows:
UCase and LCaseChange the case of text to all uppercase or all lowercase, respectively
InStr and InStrRevFind the location of one string contained within another
Left and RightRetrieve a selected number of characters from one end of a string
MidRetrieves or replaces a selected number of characters in a string
LTrim, RTrim, and TrimRemove spaces from one or both end(s) of a string
LenReturns the length of a string
Chr and AscWork with a single character's ASCII code
Str, CStr, and ValConvert a number or expression to a string, and vice versa
LenFinds and replaces within a string
StrReverseReverses the order of characters in a string
note.gif Some of the functions in this list (such as UCase) return a Variant data type. For each of them, an identical function with a dollar sign ($) at the end of the function's name indicates a String type return value. I recommend using the $ versions (such as Left$) whenever possible because they are more efficient. square.gif

In this section, you examine how you can manipulate strings to give your programs a professional appearance.
String Concatenation
As stated previously, Visual Basic supports only one string operator, the concatenation operator. This operator, which is the ampersand symbol (&), combines two or more strings of text, similar to the way the addition operator combines two or more numbers. When you combine two strings with the concatenation operator, the second string is added directly to the end of the first string. The result is a longer string containing the full contents of both source strings.
The concatenation operator is used in an assignment statement as follows:
newstring = stringexpr1 & stringexpr2 [& stringexpr3]
In this syntax, newstring represents the variable that contains the result of the concatenation operation. stringexpr1, stringexpr2, and stringexpr3 all represent string expressions. They can be any valid strings, including string variables, literal expressions (enclosed in quotation marks), or functions that return a string. The ampersand between a pair of string expressions tells Visual Basic to concatenate the two expressions. The ampersand must be preceded and followed by a space. The syntax shows an optional second ampersand and a third string expression. You can combine any number of strings with a single statement. Just remember to separate each pair of expressions with an ampersand.

 
< previous page page_183 next page >